﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using System.Xml;
using System.Xml.Xsl;

public partial class UserControl_Reports_loadReportCountWeb : System.Web.UI.UserControl
{

#region "Member variables"
    private int intUserID = 0;
    private int intModuleID = 0;
    private int intUserList = 0;
    int intChucNangID = 0;
    int intLevelID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DAHocVienLV2 objHocVienLV2;
    DAHocVienLV3LV5 objHocVienLV3LV5;
    DAHocVienLV6LV8 objHocVienLV6LV8;
    DATrangThaiGoi objTrangThaiGoi;
    DALevel objLevel;
    DANguon objNguon;
    DAHocVien objHocVien;
    DANguoidung objNguoidung;
    DATrangThai objTrangThai;
#endregion

#region "Event Method"
    public void loadLevelView()
    {
        DataSet dsLevel = objLevel.LayDanhSachLevel();
        this.ddlLevel.Items.Clear();
        ListItem liTemp = new ListItem();
        liTemp.Text = "Chọn tất cả";
        liTemp.Value = "0";
        this.ddlLevel.Items.Add(liTemp);

        foreach (DataRow dr in dsLevel.Tables[0].Rows)
        {
            liTemp = new ListItem();
            liTemp.Text = dr["Ten"].ToString();
            liTemp.Value = dr["ID"].ToString();
            this.ddlLevel.Items.Add(liTemp);
        }
    }
    public void loadNguonView()
    {
        DataSet dsNguon = objNguon.LayDanhSachNguon();
        BindData.bindCheckBoxList(this.cbxNguon, dsNguon, "ID", "Ten");
    }
    public void loadNguoidungView(CheckBoxList cbxTemp,int QuyenID)
    {
        DataSet dsNguoidung = objNguoidung.loadQuyenNguoiDungByQuyen(QuyenID,intUserID);
        BindData.bindCheckBoxList(cbxTemp, dsNguoidung, "ID", "HoTen");
    }
    public void loadTrangThaiView(int LevelID)
    {
        
        if (LevelID > 1)
        {
            DataSet dsTrangThai = objTrangThai.LayDanhSachTrangThaiID(LevelID);
            BindData.bindRadioButtonList(this.rdlTrangThai, dsTrangThai.Tables[0].Rows, "ID", "Ten");
        }
        else
        {
            this.rdlTrangThai.Items.Clear();

        }

        
    }
    private void loadTimDanhSachHocVien(int intcheck)
    {
        int intLevelID = Convert.ToInt32(this.ddlLevel.SelectedValue);
        string strNguon = "";
        string strNguoidung = "";
        string strTVTS = "";
        string strCTV = "";
        string strTrangThai = "";
        for (int i = 0; i < this.cbxNguon.Items.Count; i++)
        {
            if (this.cbxNguon.Items[i].Selected == true)
            {
                strNguon += this.cbxNguon.Items[i].Value + ",";
            }
        }
        for (int i = 0; i < this.cbxCTV.Items.Count; i++)
        {
            if (this.cbxCTV.Items[i].Selected == true)
            {
                strCTV += this.cbxCTV.Items[i].Value + ",";
            }
        }
        for (int i = 0; i < this.cbxTVTS.Items.Count; i++)
        {
            if (this.cbxTVTS.Items[i].Selected == true)
            {
                strTVTS += this.cbxTVTS.Items[i].Value + ",";
            }
        }
        for (int i = 0; i < this.rdlTrangThai.Items.Count; i++)
        {
            if (this.rdlTrangThai.Items[i].Selected == true)
            {
                strTrangThai += this.rdlTrangThai.Items[i].Value + ",";
            }
        }
        if (!strNguon.Equals(""))
        {
            strNguon = strNguon.Substring(0, strNguon.Length - 1);
        }
        if (!strCTV.Equals(""))
        {
            strCTV = strCTV.Substring(0, strCTV.Length - 1);
            strNguoidung = strCTV;
        }
        if (!strTVTS.Equals(""))
        {
            strTVTS = strTVTS.Substring(0, strTVTS.Length - 1);
            if (!strNguoidung.Equals(""))
            {
                strNguoidung += ",";
            }
            strNguoidung += strTVTS;
        }
        if (!strTrangThai.Equals(""))
        {
            strTrangThai = strTrangThai.Substring(0, strTrangThai.Length - 1);
        }
        //this.ltReport.Text = strNguon + "<BR>" + strNguoidung + "<BR>" + strTrangThai + "<BR>" + intLevelID.ToString();
        DataView dv = new DataView();
        DataSet dsHocVien = objHocVien.loadReportCountHocVienByWhere(strNguon, strNguoidung, strTrangThai, intLevelID);
        if (intcheck ==0)
        {
            if (ViewState["sortExpr"] != null)
            {
                dv = new DataView(DateTimeUntil.ConvertTable(dsHocVien.Tables[0], false));
                dv.Sort = (string)ViewState["sortExpr"];
            }
            else
            {
                dv = new DataView(DateTimeUntil.ConvertTable(dsHocVien.Tables[0], false));
            }
            this.lblKetquaView.Text = dsHocVien.Tables[0].Rows.Count.ToString();
            this.grvHocVien.DataSource = dv;
            this.grvHocVien.DataBind();
        }
        else if (intcheck ==1)
        {
            SaveExcel("DSUV" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".xls", dsHocVien.Tables[0]);
        }
        else if (intcheck == 2)
        {
            SaveWord("DSUV" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".doc", dsHocVien.Tables[0]);
        }
    }    
    public string loadReportExportExcel(DataTable dt)
    {
        try
        {
            string strHTML = "";
            strHTML += "\n  <col span=8 style='width:100%'>";
            strHTML += "\n      <tr>";
            strHTML += "\n          <td class=xl23 colspan=8>Danh Sách Ứng Viên</td>";
            strHTML += "\n      </tr>        ";
            strHTML += "\n      <tr > ";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\" >TT</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Mã ứng viên</td>";
            strHTML += "\n          <td class=xl24 >Họ tên</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Điện thoại</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Email</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Gọi gần nhất</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Trạng thái</td>";
            strHTML += "\n          <td class=xl24 >Nội dung</td>";
            strHTML += "\n      </tr>";
            foreach (DataRow dr in dt.Rows)
            {
                strHTML += "\n  <tr >";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["Seq"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["MaHocVien"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["HoTen"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["DienThoai"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["Email"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["NgayGoi"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["TenTrangThaiGoi"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["NoidungCuocGoi"].ToString() + "</td>";
                strHTML += "\n  </tr>";
            }
            strHTML = FileUntil.strFileExcel(strHTML);
            return strHTML;
        }
        catch
        {
            return "";
        }
    }
    public void SaveExcel(string strNamFile, DataTable dt)
    {
        dt.Columns.Add("NoidungCuocGoi");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            int intHocVienID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
            if (intLevelID == 2)
            {
                DataRow dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else if (intLevelID > 2 && intLevelID < 7)
            {
                DataRow dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else if (intLevelID > 6 && intLevelID < 10)
            {
                DataRow dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else
            {
                dt.Rows[i]["NoidungCuocGoi"] = "";
            }
        }

        string html = loadReportExportExcel(dt);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
        Response.Clear();
        Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        this.EnableViewState = false;
        Response.Write("\r\n");
        Response.Write(html);
        Response.End();
    }
    public void SaveWord(string strNamFile, DataTable dt)
    {
        dt.Columns.Add("NoidungCuocGoi");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            int intHocVienID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
            if (intLevelID == 2)
            {

                DataRow dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else if (intLevelID > 2 && intLevelID < 7)
            {
                DataRow dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else if (intLevelID > 6 && intLevelID < 10)
            {
                DataRow dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else
            {
                dt.Rows[i]["NoidungCuocGoi"] = "";
            }
        }

        string html = loadReportExportExcel(dt);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
        Response.Clear();
        Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-word";
        this.EnableViewState = false;
        Response.Write("\r\n");
        Response.Write(html);
        Response.End();
    }
    

#endregion
#region "Events handle"
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            loadLevelView();
            if (this.ddlLevel.Items.Count > 0)
            {
                loadTrangThaiView(Convert.ToInt32(this.ddlLevel.SelectedValue)+1);
            }
            loadNguonView();
            loadNguoidungView(this.cbxCTV,2);
            loadNguoidungView(this.cbxTVTS,5);
            loadTimDanhSachHocVien(0);
        }
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objHocVienLV2 = new DAHocVienLV2(sqlConnec);
        objHocVienLV3LV5 = new DAHocVienLV3LV5(sqlConnec);
        objHocVienLV6LV8 = new DAHocVienLV6LV8(sqlConnec);
        objTrangThaiGoi = new DATrangThaiGoi(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
        objLevel = new DALevel(sqlConnec);
        objNguon = new DANguon(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objTrangThai = new DATrangThai(sqlConnec);
    }
    protected void ddlLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlLevel.Items.Count > 0)
        {
            loadTrangThaiView(Convert.ToInt32(this.ddlLevel.SelectedValue)+1);
        }
    }

    //Su kien bin from NguoiTimNguon
    protected void grvHocVien_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
                e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
                // Modify     
                Label HocVienIDTemp = ((Label)e.Row.FindControl("HocVienID"));
                Label LevelIDTemp = ((Label)e.Row.FindControl("LevelID"));
                Label lblMaHocVienGVTemp = ((Label)e.Row.FindControl("lblMaHocVienGV"));
                if (lblMaHocVienGVTemp != null)
                {
                    string strViewMaHocVien = "JavaScript:OpenWinCenter(700, 800, '" + lblMaHocVienGVTemp.Text + "', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&HocVienID=" + HocVienIDTemp.Text + "&EventView=True');";
                    e.Row.Cells[2].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[3].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[4].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[5].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[6].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[7].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[8].Attributes["Ondblclick"] = strViewMaHocVien;
                    e.Row.Cells[9].Attributes["Ondblclick"] = strViewMaHocVien;
                }
                Label lblNoidungGVTemp = ((Label)e.Row.FindControl("lblNoidungGV"));
                // Label lblNgayGoiGanNhatGVTemp = ((Label)e.Row.FindControl("lblNgayGoiGanNhatGV"));
                if (lblNoidungGVTemp != null)
                {
                    try
                    {
                        int intHocVienID = Convert.ToInt32(HocVienIDTemp.Text);
                        if (intLevelID == 2)
                        {
                            DataRow dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
                            // lblNgayGoiGanNhatGVTemp.Text = dr["NgayGoi"].ToString();
                            lblNoidungGVTemp.Text = dr["NoiDungNgayGoi"].ToString();
                        }
                        else if (intLevelID > 2 && intLevelID < 7)
                        {
                            DataRow dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID);
                            // lblNgayGoiGanNhatGVTemp.Text = dr["NgayGoi"].ToString();
                            lblNoidungGVTemp.Text = dr["NoiDungNgayGoi"].ToString();
                        }
                        else if (intLevelID > 6 && intLevelID < 10)
                        {
                            DataRow dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID);
                            // lblNgayGoiGanNhatGVTemp.Text = dr["NgayGoi"].ToString();
                            lblNoidungGVTemp.Text = dr["NoiDungNgayGoi"].ToString();
                        }
                        else
                        {
                            // lblNgayGoiGanNhatGVTemp.Text = "Chưa có gì";
                            lblNoidungGVTemp.Text = "";
                        }
                    }
                    catch
                    {
                        // lblNgayGoiGanNhatGVTemp.Text = "Chưa có gì";
                        lblNoidungGVTemp.Text = "";
                    }
                }
            }
        }
    }
    protected void grvHocVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvHocVien.EditIndex = -1;
        this.grvHocVien.PageIndex = e.NewPageIndex;
        loadTimDanhSachHocVien(0);
    }
    protected void grvHocVien_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortOrder"] == null)
        {
            ViewState["SortOrder"] = " ASC";
        }
        else if (ViewState["SortOrder"].ToString() == " ASC")
        {
            ViewState["SortOrder"] = " DESC";
        }
        else
        {
            ViewState["SortOrder"] = " ASC";
        }
        ViewState["sortExpr"] = e.SortExpression + ViewState["SortOrder"];
        loadTimDanhSachHocVien(0);
    }

    protected void btnExportFile_Click(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien(1);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien(0);
    }
    protected void btnExportFileWord_Click(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien(2);
    }
#endregion
}
